package com.web709.controller;

import com.web709.common.R;
import com.web709.entity.Article;
import com.web709.service.ArticleService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/articles")
@Api(tags = "资讯管理接口", value = "资讯的发布、查询及管理")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/publish")
    @ApiOperation(value = "发布新资讯",
            notes = "管理员发布新的资讯文章，包含标题、内容、封面图等信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "article",
                    value = "资讯文章对象",
                    required = true,
                    dataTypeClass = Article.class,
                    paramType = "body"),
            @ApiImplicitParam(name = "token",
                    value = "管理员token",
                    required = true,
                    paramType = "header",
                    dataType = "String")
    })
    public R<String> publish(@RequestBody Article article) {
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setViewCount(0);
        article.setStatus(1);  // 直接设置为已发布状态
        articleService.save(article);
        return R.success("发布成功");
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取资讯列表",
            notes = "分页查询所有已发布的资讯，默认按创建时间倒序排列")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",
                    value = "页码（从1开始）",
                    defaultValue = "1",
                    dataType = "Integer",
                    paramType = "query"),
            @ApiImplicitParam(name = "size",
                    value = "每页数量",
                    defaultValue = "10",
                    dataType = "Integer",
                    paramType = "query"),
            @ApiImplicitParam(name = "type",
                    value = "资讯类型：1-健康资讯 2-活动新闻 3-其他",
                    required = false,
                    dataType = "Integer",
                    paramType = "query")
    })
    public R<List<Article>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer type) {
        List<Article> articles = articleService.listByPage(page, size, type);
        return R.success(articles);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取资讯详情",
            notes = "根据资讯ID获取完整资讯内容")
    @ApiImplicitParam(name = "id",
            value = "资讯ID",
            required = true,
            dataType = "Integer",
            paramType = "path")
    public R<Article> detail(@PathVariable Integer id) {
        Article article = articleService.getById(id);
        // 增加浏览次数
        article.setViewCount(article.getViewCount() + 1);
        articleService.updateById(article);
        return R.success(article);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新资讯信息",
            notes = "更新资讯的标题、内容、封面图等信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "article",
                    value = "资讯信息",
                    required = true,
                    dataType = "Article"),
            @ApiImplicitParam(name = "token",
                    value = "管理员token",
                    required = true,
                    paramType = "header",
                    dataType = "String")
    })
    public R<String> update(@RequestBody Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleService.updateById(article);
        return R.success("更新成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除单个资讯",
            notes = "根据资讯ID删除单个资讯")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",
                    value = "资讯ID",
                    required = true,
                    dataType = "Integer",
                    paramType = "path"),
            @ApiImplicitParam(name = "token",
                    value = "管理员token",
                    required = true,
                    paramType = "header",
                    dataType = "String")
    })
    public R<String> delete(@PathVariable Integer id) {
        articleService.removeById(id);
        return R.success("删除成功");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "批量删除资讯",
            notes = "根据资讯ID列表批量删除资讯")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",
                    value = "资讯ID列表",
                    required = true,
                    dataType = "List",
                    paramType = "body"),
            @ApiImplicitParam(name = "token",
                    value = "管理员token",
                    required = true,
                    paramType = "header",
                    dataType = "String")
    })
    public R<String> batchDelete(@RequestBody List<Integer> ids) {
        articleService.removeByIds(ids);
        return R.success("批量删除成功");
    }

    @GetMapping("/search")
    @ApiOperation(value = "搜索资讯",
            notes = "根据关键词搜索资讯的标题和内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword",
                    value = "搜索关键词",
                    required = true,
                    dataType = "String",
                    paramType = "query"),
            @ApiImplicitParam(name = "page",
                    value = "页码（从1开始）",
                    defaultValue = "1",
                    dataType = "Integer",
                    paramType = "query"),
            @ApiImplicitParam(name = "size",
                    value = "每页数量",
                    defaultValue = "10",
                    dataType = "Integer",
                    paramType = "query")
    })
    public R<List<Article>> search(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        List<Article> articles = articleService.searchArticles(keyword, page, size);
        return R.success(articles);
    }
}